package ro.gateway.aida.sectors;

import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.AIDASector;
import ro.gateway.aida.obj.db.AIDASectorPersMan;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.Perms;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.Utils;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;



/**
 * Created by IntelliJ IDEA.
 * User: xblue
 * Date: Dec 16, 2003
 * Time: 11:26:54 AM
 * To change this template use Options | File Templates.
 */
public class ViewSectorsAction extends Action
{
      public ActionForward execute(
            ActionMapping mapping,
            ActionForm _form,
            HttpServletRequest request,
            HttpServletResponse response )
            throws Exception
      {

            ServletContext application = this.getServlet().getServletContext();
            PersistenceToken token = PersistenceManager.
                  tokenLookup( application );

            Arbiter ar = Arbiter.getManager( token );
            User logged_user = UserUtils.getLogged( request, response );
            boolean has_rights =
                  ( logged_user != null ) &&
                  ( ar.canDo( request, Perms.ADMIN_TOOLS ) );
            if ( !has_rights )
            {
                  return mapping.findForward( "norights" );
            }

            PersistenceToken pers_token = PersistenceManager.
                  tokenLookup( application );
            AIDASectorPersMan pers_man = AIDASectorPersMan.
                  getManager( pers_token );

            LanguageBean lang_bean = TranslatorUtils.
                  getCurrentLanguage( request, application );
            ArrayList sectors = new ArrayList();
            try
            {
                  AIDASector[] roots = pers_man.getAllRoots( );
                  for ( int i = 0; i < roots.length; i++ )
                  {
                        sectors.add( roots[ i ] );
                        AIDASector[] children = pers_man.
                              getAllForParent( roots[ i ].getRo_aida_code() );
                        if ( children != null )
                        {
                              for ( int j = 0; j < children.length; j++ )
                              {
                                    children[ j ].setParent( roots[ i ] );
                                    sectors.add( children[ j ] );
                                    AIDASector[] nepoti = pers_man.
                                          getAllForParent(children[j].getRo_aida_code());
                                    if (nepoti != null) {
                                          for (int k = 0; k < nepoti.length; k++) {
                                                nepoti[k].setParent(children[j]);
                                                sectors.add(nepoti[k]);
                                          }
                                    }
                              }
                        }
                  }
            }
            catch ( Exception ex )
            {
                  ex.printStackTrace();
            }


            if ( sectors.size() > 0 )
            {
                  AIDASector[] sectors_arr = new AIDASector[ sectors.size() ];
                  sectors.toArray( sectors_arr );
                  request.setAttribute( "sectors", SectorWrapper.wrapp( sectors_arr, lang_bean.language ) );
            }

            Utils.setBreadCrumbs( request, "bc.sectors.browser" );
            return mapping.findForward( "jsp_page" );
      }
}
